package cn.edu.scau.cmi.chenjiale.integration;

import java.util.List;

import org.springframework.context.ApplicationContext;

import cn.edu.scau.cmi.chenjiale.integration.dao.StudentDao;
import cn.edu.scau.cmi.chenjiale.integration.dao.TeacherDao;
import cn.edu.scau.cmi.chenjiale.integration.entity.Student;
import cn.edu.scau.cmi.chenjiale.integration.entity.Teacher;
import cn.edu.scau.cmi.chenjiale.spring.util.ApplicationContextUtil;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * 主启动类
 * 
 * @author chen.jiale
 * @date 2019/05/18
 *
 */
public class IntegrationApplication extends Application {
	private ApplicationContext context;
	private TeacherDao teacherDao;
	private StudentDao studentDao;

	@Override
	public void start(Stage primaryStage) throws Exception {
		context = ApplicationContextUtil.getApplicationContext();
		teacherDao = context.getBean(TeacherDao.class);
		studentDao = context.getBean(StudentDao.class);

		BorderPane pane = new BorderPane();
		HBox btnPane = new HBox(5);
		Button btn1 = new Button("显示所有学生");
		Button btn2 = new Button("显示所有教师");
		btnPane.getChildren().addAll(btn1, btn2);
		TextArea contentPane = new TextArea();
		pane.setTop(btnPane);
		pane.setCenter(contentPane);

		primaryStage.setScene(new Scene(pane, 400, 400));
		primaryStage.setTitle("spring整合hibernate");
		primaryStage.show();

		btn1.setOnAction(e -> {
			contentPane.clear();
			List<Student> studentList = studentDao.list();
			for (Student student : studentList) {
				contentPane.appendText("学号:  " + student.getId() + ",姓名:  " + student.getName() + "\n");
			}
		});

		btn2.setOnAction(e -> {
			contentPane.clear();
			List<Teacher> teacherList = teacherDao.list();
			for (Teacher teacher : teacherList) {
				contentPane.appendText("工号:  " + teacher.getId() + ",姓名:  " + teacher.getName() + "\n");
			}
		});

	}

	public static void main(String[] args) {
		Application.launch(args);
	}

}
